package com.qms.repast.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.qms.repast.App;
import com.qms.repast.R;
import com.qms.repast.commons.GlideApp;
import com.qms.repast.db.CuisineOrderBeanDao;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.CuisineBean;
import com.qms.repast.entity.resbean.CuisineOrderBean;
import com.qms.repast.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/8/9 0009
 * @describe: 订单确认适配器
 */
public class OrderReviewAdapter extends RecyclerView.Adapter<OrderReviewAdapter.ViewHolder> {


	private Context context;
	private List<CuisineBean> data;
	private String userId;
	private String shopId;

	public OrderReviewAdapter (Context context) {
		this.context = context;
		data = new ArrayList<>();
	}

	public void setList (List<CuisineBean> list, String userId, String shopId) {
		this.shopId = shopId;
		this.userId = userId;
		data.clear();
		data.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(context).inflate(R.layout.layout_order_review_item, parent, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder (final ViewHolder holder, final int position) {
		final CuisineBean cuisineBean = data.get(position);
		holder.tvNum.setText(cuisineBean.getOptNum() + "");
		holder.tvName.setText(cuisineBean.getTitle());
		holder.tvPrice.setText("￥" + cuisineBean.getPrice() + "元");
		holder.tvSum.setText("X" + cuisineBean.getOptNum());
		GlideApp.with(context)
				.load(cuisineBean.getIcon())
				.placeholder(R.mipmap.default_pic)
				.apply(new RequestOptions()
							   .transforms(new CenterCrop(), new RoundedCorners(10)
							   ))
				.into(holder.ivImage);

		holder.ivSub.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				int optNum = cuisineBean.getOptNum();
				if (optNum > 1) {
					optNum--;
					List<CuisineOrderBean> cuisineOrderBeans = App.getmDaoSession()
							.getCuisineOrderBeanDao()
							.queryBuilder()
							.limit(1)
							.where(
									CuisineOrderBeanDao.Properties.UserId.eq(userId),
									CuisineOrderBeanDao.Properties.ShopId.eq(shopId),
									CuisineOrderBeanDao.Properties.ItemId.eq(cuisineBean.getItemId())
							).orderDesc(CuisineOrderBeanDao.Properties.Id).list();

					App.getmDaoSession()
							.getCuisineOrderBeanDao().delete(cuisineOrderBeans.get(0));
					cuisineBean.setOptNum(optNum);
					holder.tvSum.setText("X" + cuisineBean.getOptNum());
					notifyItemChanged(position);
					EventBus.getDefault().post(new MessageEvent<>(1211, "删除一条"));
				} else {
					ToastUtils.showToast("数量不能再少了！");
				}

			}
		});
		holder.ivAdd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				int optNum = cuisineBean.getOptNum();
				optNum++;
				cuisineBean.setOptNum(optNum);
				holder.tvSum.setText("X" + cuisineBean.getOptNum());
				notifyItemChanged(position);

				CuisineOrderBean cuisineOrderBean = new CuisineOrderBean();
				cuisineOrderBean.setUserId(userId);
				cuisineOrderBean.setShopId(shopId);
				cuisineOrderBean.setItemId(cuisineBean.getItemId());
				cuisineOrderBean.setItemNum(1);
				App.getmDaoSession().getCuisineOrderBeanDao().insert(cuisineOrderBean);
				EventBus.getDefault().post(new MessageEvent<>(1211, "增加一条"));
			}
		});
		holder.item_slide.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View view) {
				data.remove(position);
				notifyDataSetChanged();
				List<CuisineOrderBean> cuisineOrderBeans = App.getmDaoSession()
						.getCuisineOrderBeanDao()
						.queryBuilder()
						.where(
								CuisineOrderBeanDao.Properties.UserId.eq(userId),
								CuisineOrderBeanDao.Properties.ShopId.eq(shopId),
								CuisineOrderBeanDao.Properties.ItemId.eq(cuisineBean.getItemId())
						).orderDesc(CuisineOrderBeanDao.Properties.Id).list();
				for (CuisineOrderBean bean : cuisineOrderBeans) {
					App.getmDaoSession()
							.getCuisineOrderBeanDao().delete(bean);
				}
				EventBus.getDefault().post(new MessageEvent<>(1211, "删除一类"));
			}
		});
	}

	@Override
	public int getItemCount () {
		return data.size();
	}


	/**
	 * view.getWidth()获取的是屏幕中可以看到的大小.
	 */
	public class ViewHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.ll_content)
		public LinearLayout llContent;
		@BindView(R.id.item_slide)
		public TextView item_slide;
		@BindView(R.id.tv_num)
		public TextView tvNum;
		@BindView(R.id.iv_add)
		public ImageView ivAdd;
		@BindView(R.id.iv_sub)
		public ImageView ivSub;
		@BindView(R.id.iv_image)
		public ImageView ivImage;
		@BindView(R.id.tv_name)
		public TextView tvName;
		@BindView(R.id.tv_price)
		public TextView tvPrice;
		@BindView(R.id.tv_sum)
		public TextView tvSum;

		public ViewHolder (View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}

	}

	/**
	 * 根据手机分辨率从DP转成PX
	 *
	 * @param context
	 * @param dpValue
	 *
	 * @return
	 */
	public static int dip2px (Context context, float dpValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
